/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：GoodsTypeContorller.java
 * History:
 *       May 13, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.contorller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.entities.BaseGoodsType;
import com.xteam.ecsystem.service.admin.GoodsTypeService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 商品类型控制枢纽站
 * @author liuzhi
 *
 */
@Controller
public class GoodsTypeContorller {

	@Autowired
	private GoodsTypeService goodsTypeService;
	
	@Autowired
	private SystemLogService logService;
	
	public void setToBeJsonType(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/json;charset=UTF-8");
	};
	
	@RequestMapping("/getGoodsTypeList.do")
	public void getGoodsTypeList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String typeName = ReqHelper.getRequestString(request, "typeName");
		String typeState = ReqHelper.getRequestString(request, "typeState");
		
		String strList = this.goodsTypeService.getGoodsTypeList(pageIndex, pageSize, typeName, typeState);
		response.getWriter().write(strList);
	}
	
	@RequestMapping("/saveOrUpdateGoodsType.do")
	public void saveOrUpdateObject(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String jsonStr = ReqHelper.getRequestString(request, "goodsTypeInfo");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD;
		
		JSONObject obj = JSONObject.fromObject(jsonStr);
		BaseGoodsType goodsType = (BaseGoodsType)JSONObject.toBean(obj, BaseGoodsType.class);
		
		if(StringUtil.isNotNullOrEmpty(goodsType.getBgtId())){
			lslType = ConstantLocation.LOG_MODIFY;
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSTYPE_MGR, 
				"saveOrUpdateObject", lslType, "BaseGoodsType");
		
		this.goodsTypeService.saveOrUpdateObject(goodsType, goodsType.getBgtId());
	}
	
	@RequestMapping("/getGoodsTypeInfoById.do")
	public void getGoodsTypeInfoById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bgtId = ReqHelper.getRequestString(request, "bgtId");		
		String strInfo = this.goodsTypeService.getGoodsTypeInfoById(bgtId);
		
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/updateGoodsTypeStateById.do")
	public void updateGoodsTypeStateByIds(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bgtId = ReqHelper.getRequestString(request, "bgtId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.goodsTypeService.updateGoodsTypeStateById(bgtId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSTYPE_MGR, 
				"updateGoodsTypeStateById", ConstantLocation.LOG_Edit_STATE, "BaseGoodsType");

	}
	
	@RequestMapping("/delGoodsTypeByIds.do")
	public void delGoodsTypeByIds(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bgtIds = ReqHelper.getRequestString(request, "bgtIds");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.goodsTypeService.delObject(bgtIds);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSTYPE_MGR, 
				"delObject", ConstantLocation.LOG_DELETE, "BaseGoodsType");

	}
	
	@RequestMapping("/getBgiByBgt.do")
	public void getBgiByBgt(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bgtIds = ReqHelper.getRequestString(request, "bgtIds");

		boolean isUsed = this.goodsTypeService.getBgiByBgt(bgtIds);
		if (isUsed) {
			response.getWriter().write("1");
		}else{
			response.getWriter().write("0");
		}
	}
	
}
